using DealerPlatform.Common.EventBusHelper;
using DealerPlatform.Core.Core;
using DealerPlatform.Core.Repository;
using DealerPlatform.Extensions;
using DealerPlatform.Service;
using DealerPlatform.Service.CustomerApp;
using DealerPlatform.Web.Controllers.Extensions;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.EntityFrameworkCore;

var builder = WebApplication.CreateBuilder(args);

// IConfigurationBuilder cfgBuilder = new ConfigurationBuilder()
//     .SetBasePath(Directory.GetCurrentDirectory())
//     .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
// IConfiguration config = cfgBuilder.Build();


IConfiguration config = builder.Configuration.SetBasePath(Directory.GetCurrentDirectory())
                        .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
                        .Build();

// IConfiguration config2 = builder.Services.GetConfiguration();

builder.Services.AddDbContext<DealerPlatformContext>(opt=>{
    opt.UseSqlServer(config.GetConnectionString("Default"));
});

builder.Services.AddCors(c=>c.AddPolicy("any", p=>p.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod()));
builder.Services.MyAddAuthentication(config, true);
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.MyAddSwaggerGen("DealerPlatform.Web");
// builder.Services.AddTransient(typeof(IRepository<>),typeof(Repository<>));
// builder.Services.AddTransient<ICustomerService, CustomerService>();   
builder.Services.RepositoryRegister();
builder.Services.ServicesRegister(); 

// 第一步：安装Nuget包 AutoMapper 和 AutoMapper.Extensions.Microsoft.DependencyInjection
// 第二步：创建映射类
// 第三步：将AutoMapper注册到容器
builder.Services.AddAutoMapper(typeof(DealerPlatformProfile));
builder.Services.AddScoped(typeof(LocalEventBus<>));


var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseCors("any");
app.UseAuthentication();
app.MapControllers();
app.UseHttpsRedirection();


app.Run();

